<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link type="text/css" href="ui.all.css" rel="stylesheet" />
<script language="javascript" type="text/javascript" src="AIRAliases.js"></script> 
<script type="text/javascript" src="jqueryUI/jquery-1.3.2.js"></script>
<script type="text/javascript" src="jqueryUI/ui/ui.core.js"></script>
<script type="text/javascript" src="jqueryUI/ui/ui.progressbar.js"></script>


  
<style type="text/css">
#upload {
	width: 500px;
	height: 200px;
	background:#cccccc;	
	}
</style>
</head>

<body ondragenter="preventDefault(event)" ondragover="preventDefault(event)">
<div id="upload">
把你想要上传的文件拖动到这里
</div>
<div id="progressbar"></div>
<div id="uploadProgress">上传进度</div>
<div id="info"></div>
  <script language="javascript">  
  /*window.htmlLoader.addEventListener("nativeDragDrop",function(event){  
	  var filelist = event.clipboard.getData(air.ClipboardFormats.FILE_LIST_FORMAT);  
	  //air.trace(filelist[0].url);  
	  for ( var prop in filelist ) {
		  alert(filelist[prop].url);
		}	  
  }); */
  document.getElementById("upload").addEventListener("drop", dropHandler);
  filesToUpload = new Array();
  
  function dropHandler(event){
	  //alert(event.dataTransfer.types[0])
	  var filelist = event.dataTransfer.getData(event.dataTransfer.types[0]);  
	  //air.trace(filelist[0].url);  
	  
	  for ( var prop in filelist ) {
		  //alert(filelist[prop].url);
		  var urlStr = filelist[prop].url;
		  var file = new air.File(urlStr);
		  //alert(file.size);
		  filesToUpload.push(file);
		}	 
		loginIn();
	}
	
	function loginIn() {
		var user = "jiee2008";
		var password = "history";
		
		var url = new air.URLRequest("http://www.blogbus.com/user/login.php");
		var loader = new air.URLLoader();	
		url.method =  air.URLRequestMethod.POST;
		url.data = "username=" + user + "&password=" + password;
		

		loader.addEventListener(air.Event.COMPLETE, function() {
															//alert(loader.data);
															/* to see is logged in*/
															pattern = /id="logpage"/g;
															if (pattern.test(loader.data)) {
																/* haven't logged in,display login form*/
																//$("#loginForm").fadeIn();
																$("#info").html("<span>登录失败，请检查用户名及密码是否正确！</span>");
															} else {
																//$("#loginForm").fadeOut();	
																$("#info").html("<span>登录成功，您可以进行接下来的操作了.</span>");	
																//$("#postForm").fadeIn();
																//uploadToBlogbus();
																//for (var i=0; i<filesToUpload.length; i++) 
																	//alert(filesToUpload[i].name);
																/*for (var f in filesToUpload) 
																	alert(filesToUpload[f].name);*/
																uploadToBlogbus( filesToUpload );
															}															
														});
		loader.load( url );	
	}
	
	function uploadToBlogbus( files ) {
		var url = new air.URLRequest("http://file.home.blogbus.com/");
		var loader = new air.URLLoader();
		airReferer = new air.URLRequestHeader("Referer","http://www.blogbus.com/user/");
		url.requestHeaders = new Array(airReferer);
		loader.addEventListener(air.Event.COMPLETE, function() {
						var url2 = new air.URLRequest("http://file.home.blogbus.com/documents/1792454/upload");
						url2.method =  air.URLRequestMethod.POST;
						for ( var f in files ) {
							//var file = filesToUpload[f];
							//if ( filesToUpload[f].size > 5242880 ) { alert(filesToUpload[f].name+"'s files size is more than 5MB!");continue;}
							filesToUpload[f].addEventListener(air.ProgressEvent.PROGRESS,function(event) {
																						document.getElementById("uploadProgress").innerHTML =  document.getElementById("uploadProgress").innerHTML + Math.round(event.bytesLoaded/event.bytesTotal * 100) + "-";
																						//$('#progressbar').progressbar({ value: 0 });
																						//$("#progressbar").progressbar('option', 'value', Math.round(event.bytesLoaded/event.bytesTotal * 100));
																				});							
							filesToUpload[f].addEventListener(air.Event.COMPLETE, function() { alert(files[f].name+" has finished!");
																								//getUploadItems();
																											 }); 
							filesToUpload[f].upload(url2);				
						}

						//pattern = /<div\sclass="info">[\s\S]*?<\/div>/g;
						/*pattern = /<p>[\s\S]*?\/documents\/\d+\/items[\s\S]*?<\/p>/g;
						alert(loader.data.match(pattern));*/
															 });
		loader.load(url);
	}
	
	function getUploadItems() {
		
		var url = new air.URLRequest("http://file.home.blogbus.com/documents/1792454/thumbs?page=2");
		var loader = new air.URLLoader();
		airReferer = new air.URLRequestHeader("Referer","http://www.blogbus.com/user/");
		url.requestHeaders = new Array(airReferer);			
		loader.addEventListener(air.Event.COMPLETE, function() {
															 //pattern = /<td\sclass="tdCenter"[\s\S]*?[\s\S]*?<\/td>/g ;
															 pattern = /<li>[\s\S]*?href="([\s\S]*?)"[\s\S]*?title="([\s\S]*?)"[\s\S]*?(?:img[\s\S]*?){2}src="([\s\S]*?)"[\s\S]*?<\/li>/g;
															 pattern2 = /<li>[\s\S]*?href="([\s\S]*?)"[\s\S]*?title="([\s\S]*?)"[\s\S]*?(?:img[\s\S]*?){2}src="([\s\S]*?)"[\s\S]*?<\/li>/;
															 //alert( loader.data.match(pattern).length ) ;
															 for ( var i in loader.data.match(pattern) )
															 	$("#info").append("<a href='"+loader.data.match(pattern)[i].match(pattern2)[1] +"'><img src='"+loader.data.match(pattern)[i].match(pattern2)[3]+"' /></a>");
															 } );
		loader.load(url);
	}
	//loginIn();
	//getUploadItems();
	
  </script> 
  	<script type="text/javascript">
	/*$(function() {
		$("#progressbar").progressbar({	value: 34 });
	});*/
	</script>
  <script language="javascript" type="text/javascript">  
  function preventDefault(event){  
  	event.preventDefault();  
  }  
  </script>
</body>
</html>
